package com.wootion.dizhi.controller;
import com.alibaba.fastjson.JSONObject;
import com.wootion.dizhi.entity.ConfigType;
import com.wootion.dizhi.entity.Cylinder;
import com.wootion.dizhi.exception.BusinessException;
import com.wootion.dizhi.response.Result;
import com.wootion.dizhi.service.DataCollectionService;
import com.wootion.dizhi.service.RunDataService;
import com.wootion.dizhi.service.SseService;
import com.wootion.dizhi.vo.Condition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalTime;
import java.util.List;

/**
 * 数据收集(液位计，气象色普怡,类型设置)
 */
@Api(tags = "数据收集")
@RestController
@RequestMapping("/dataCollection")
public class DataCollectionController {
    @Resource
    private DataCollectionService dataCollectionService;
    @Resource
    private RunDataService runDataService;
    @Resource
    SseService sseService;

    /**
     * 开始记录
     * @param jsonObject
     * @return
     * @throws JAXBException
     * @throws IOException
     */
    @PostMapping("/start")
    @ApiOperation("开始记录")
    public Result start(@RequestBody JSONObject jsonObject) throws JAXBException, IOException {

        return  runDataService.start(jsonObject);
    }

    /**
     * 结束计划
     * @param jsonObject
     * @return
     * @throws JAXBException
     * @throws IOException
     */
    @ApiOperation("结束计划")
    @PostMapping("/end")
    public Result end(@RequestBody JSONObject jsonObject) throws JAXBException, IOException {
        return runDataService.end(jsonObject);
    }

    /**
     * 获取设备状态
     * @return
     * @throws JAXBException
     */
    @GetMapping("/getDeviceStatus")
    @ApiOperation("获取设备状态")
    public Result getDeviceStatus() throws JAXBException {
        return runDataService.getDeviceStatus();
    }

    /**
     * 保存设备记录
     * @param jsonObject
     * @return
     * @throws JAXBException
     * @throws IOException
     */
    @PostMapping("/save")
    @ApiOperation("保存设备记录")
    public Result save(@RequestBody JSONObject jsonObject) throws JAXBException, IOException {

        return runDataService.save(jsonObject);
    }

    /**
     * 获取配置
     * @param jsonObject
     * @return
     */
    @PostMapping("/getConfig")
    @ApiOperation("获取配置")
    public Result getConfig (@RequestBody JSONObject jsonObject) throws JAXBException, IOException {
        return runDataService.getConfig(jsonObject);
    }

    @GetMapping("/getWarning")
    @ApiOperation("获取告警列表")
    public Result getWarnings(@RequestParam(defaultValue = "0") int pageNo,
                                     @RequestParam(defaultValue = "10") int pageSize,
                                  @RequestParam(required = false) String address) {
        int offset = pageNo * pageSize;
        return runDataService.selectAllWithPagination(offset, pageSize,address);
    }


    /**
     * 周期提供警告列表和设备状态
     * @return
     */
    @GetMapping("/ceegetwarning")
    @ApiOperation("周期提供警告列表和设备状态")
    public SseEmitter ceegetwarning() {
       return sseService.createEmitter();
    }

    /**
     * 消息已读
     * @return
     */
    @GetMapping("/isread")
    @ApiOperation("历史样品-详情-基本信息")
    public Result isread(@RequestParam(value = "id")String id){
        return dataCollectionService.isread(id);
    }

    @PostMapping("/predictionStartCount")
    @ApiOperation(value = "区块预测-开始计算",notes = "传返回对象数组,其中关于对象,返回name为区块信息,返回数组infos作为比对中信息")
    public Result predictionCreate(@ApiParam("传递文件路径path") @RequestBody JSONObject jsonObject){
        return dataCollectionService.predictionCreate(jsonObject);
    }

//    @PostMapping("/predictionInfo")
//    @ApiOperation(value = "区块预测-详情",notes = "传返回对象数组,其中关于对象,返回name为区块信息,返回数组infos作为比对中信息")
//    public Result predictionCreate(@ApiParam("传递文件路径path") @RequestBody JSONObject jsonObject){
//        return dataCollectionService.predictionCreate(jsonObject);
//    }
//-----------------------------------------------------------------------------------------------------------------


    /**
     * 历史样品列表
     * @return
     */
    @PostMapping("/historicalList")
    @ApiOperation("历史样品列表")
    public Result historicalList(@RequestBody @Validated Condition condition){
        return dataCollectionService.historicalList(condition);
    }

    /**
     * 历史样品-详情-基本信息
     * @return
     */
    @GetMapping("/historicalBasicInfo")
    @ApiOperation("历史样品-详情-基本信息")
    public Result historicalBasicInfo(@RequestParam(value = "id")String id){
        return dataCollectionService.historicalBasicInfo(id);
    }

    /**
     * 历史样品-详情-曲线图
     * @return
     */
    @GetMapping("/historicalGraph")
    @ApiOperation("历史样品-详情-曲线图")
    public Result historicalGraph(@RequestParam(value = "id")String id,
                                  @RequestParam(value = "hour",required = false)String hour){
        return dataCollectionService.historicalGraph(id,hour);
    }

    /**
     * 历史样品-详情-原始数据
     * @return
     */
    @PostMapping("/historicalRawData")
    @ApiOperation("历史样品-详情-原始数据")
    public Result historicalRawData(@RequestBody @Validated Condition condition){
        return dataCollectionService.historicalRawData(condition);
    }

    /**
     * 历史样品-导出数据
     * @return
     */
    @GetMapping("/historicalExport")
    @ApiOperation("历史样品-导出数据")
    public Result historicalExport(@RequestParam(value = "id")String id){
        return dataCollectionService.historicalExport(id);
    }

    /**
     * 现在读数-原始数据
     * @return
     */
    @PostMapping("/rawData")
    @ApiOperation("现在读数-原始数据")
    public Result rawData(@RequestBody Condition condition){
        return dataCollectionService.rawData(condition);
    }

    /**
     * 现在读数-曲线图
     * @return
     */
    @GetMapping("/graph")
    @ApiOperation("现在读数-曲线图")
    public Flux<ServerSentEvent<Result>> graph(
                               @RequestParam("address")String address,
                               @RequestParam("experiment")String experiment) {
        return Flux.interval(Duration.ofSeconds(6))
                .map(seq -> {
                    System.out.println("现在读数-曲线图--开始执行业务逻辑，去查询数据库");
                    Result result = dataCollectionService.graph(address, experiment);
                    return ServerSentEvent.<Result>builder()
                            .data(result)
                            .build();
                }).startWith(ServerSentEvent.<Result>builder()
                        .data(dataCollectionService.graph(address, experiment))
                        .build());
    }
    /**
     * 现在读数-曲线图
     * @return
     */
    @GetMapping("/graphHour")
    @ApiOperation("现在读数-曲线图-某小时之内的分钟曲线图")
    public Flux<ServerSentEvent<Result>> graphHour(
            @RequestParam("address")String address,
            @RequestParam("experiment")String experiment,
            @RequestParam("hour")String hour) {
        return Flux.interval(Duration.ofSeconds(6))
                .map(seq -> {
                    System.out.println("现在读数-曲线图--某小时之内的分钟曲线图,开始执行业务逻辑，去查询数据库");
                    Result result = dataCollectionService.graphHour(address, experiment, hour);
                    return ServerSentEvent.<Result>builder()
                            .data(result)
                            .build();
                }).startWith(ServerSentEvent.<Result>builder()
                        .data(dataCollectionService.graphHour(address, experiment,hour))
                        .build());
    }

    /**
     * 新增配置类型
     * @param configType
     * @return
     */
    @PostMapping("/insertConfigType")
    @ApiOperation("新增配置类型")
    public Result insertConfigType(@RequestBody ConfigType configType){
        return dataCollectionService.insertConfigType(configType);
    }

    /**
     * 根据id查询配置类型
     * @param id
     * @return
     */
    @GetMapping("/findByConfigTypeId")
    @ApiOperation("根据id查询配置类型")
    public Result findByConfigTypeId(@RequestParam(value = "id")String id){
        return dataCollectionService.findByConfigTypeId(id);
    }

    /**
     * 根据id删除配置类型
     * @param id
     * @return
     */
    @DeleteMapping("/deleteConfigTypeId")
    @ApiOperation("根据id删除配置类型")
    public Result deleteConfigTypeId(@RequestParam(value = "id")String id){
        return dataCollectionService.deleteConfigTypeId(id);
    }

    /**
     * 更新配置类型
     * @param configType
     * @return
     */
    @PutMapping("/updateConfigType")
    @ApiOperation("更新配置类型")
    public Result updateConfigType(@RequestBody ConfigType configType){
        return dataCollectionService.updateConfigType(configType);
    }

    /**
     * 配置类型列表查询
     * @param condition
     * @return
     */
    @PostMapping("/configTypeList")
    @ApiOperation("配置类型列表查询")
    public Result configTypeList(@RequestBody Condition condition){
        return dataCollectionService.configTypeList(condition);
    }

    /**
     * 配置类型下拉选项
     * @param
     * @return
     */
    @GetMapping("/configTypeListOptions")
    @ApiOperation("配置类型下拉选项")
    public Result configTypeListOptions(){
        return dataCollectionService.configTypeListOptions();
    }

    /**
     * 气象色谱仪列表
     * @param
     * @return
     */
    @PostMapping("/atmosphereList")
    @ApiOperation("气象色谱仪列表")
    public Result atmosphereList(@RequestBody @Validated Condition condition){
        return dataCollectionService.atmosphereList(condition);
    }

    /**
     * 气象色谱仪-导入数据
     * @param
     * @return
     */
    @PostMapping("/atmosphereExport")
    @ApiOperation("气象色谱仪-导入数据")
    public Result atmosphereExport(@RequestParam("excelFile")MultipartFile multipartFile) throws BusinessException {
        return dataCollectionService.atmosphereExport(multipartFile);
    }

    /**
     * 气象色谱仪-保存导入的数据
     * @param
     * @return
     */
    @PostMapping("/atmosphereExportSave")
    @ApiOperation("气象色谱仪-保存导入的数据")
    public Result atmosphereExportSave(@RequestParam(value = "resId")String resId,
                                       @RequestBody List<Cylinder> cylinders){
        return dataCollectionService.atmosphereExportSave(resId,cylinders);
    }

    /**
     * 气象色谱仪-详情-原始数据分页列表
     * @param
     * @return
     */
    @PostMapping("/atmosphereExportList")
    @ApiOperation("气象色谱仪-详情-原始数据分页列表")
    public Result atmosphereExportList(@RequestBody Condition condition){
        return dataCollectionService.atmosphereExportList(condition);
    }

    /**
     * 气象色谱仪-详情-曲线图
     * @param
     * @return
     */
    @GetMapping("/atmosphereExportGraph")
    @ApiOperation("气象色谱仪-详情-曲线图")
    public Result atmosphereExportGraph(@RequestParam(value = "id")String id){
        return dataCollectionService.atmosphereExportGraph(id);
    }

    /**
     * 气象色谱仪-导出数据
     * @param
     * @return
     */
    @GetMapping("/atmosphereImport")
    @ApiOperation("气象色谱仪-导出数据")
    public Result atmosphereImport(@RequestParam(value = "id")String id, HttpServletResponse response){
        return dataCollectionService.atmosphereImport(id,response);
    }

}
